/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.client.screen.testscreen.producteditor;

import com.daikit.daikit4gxt.client.action.DkBaseAction;
import com.daikit.daikit4gxt.client.screen.DkBaseScreenSubPanel;
import com.daikit.daikit4gxt.client.ui.forms.DkTabPanel;
import com.daikit.daikit4gxtsample.client.Main;
import com.daikit.daikit4gxtsample.shared.bean.ProductData;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.TabPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.toolbar.FillToolItem;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;


/**
 * Product Editor UI.
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public abstract class ProductEditorUI extends DkBaseScreenSubPanel
{

	private final TextButton buttonSave;
	private final TextButton buttonTest;
	private final DkTabPanel tabPanel;
	private final ToolBar toolbar;

	private final ProductEditorController controller;

	/**
	 * Constructor
	 */
	public ProductEditorUI()
	{
		setHeaderVisible(false);
		setBorders(false);
		setBodyBorder(false);

		this.controller = new ProductEditorController(this)
		{
			@Override
			protected void onProductChanged(final ProductData productData)
			{
				ProductEditorUI.this.onProductChanged(productData);
			}
		};

		toolbar = new ToolBar();

		buttonSave = new TextButton(Main.i18n().label_save(), new SelectEvent.SelectHandler()
		{
			@Override
			public void onSelect(final SelectEvent event)
			{
				controller.doSave();
			}
		});
		buttonSave.setIcon(Main.icons().save_16());
		toolbar.add(new FillToolItem());
		toolbar.add(buttonSave);

		buttonTest = new TextButton("TEST", new SelectEvent.SelectHandler()
		{
			@Override
			public void onSelect(final SelectEvent event)
			{
				controller.setTab4Visible(!controller.isTab4Visible());
				Main.controller().invalidateUi(false);
			}
		});
		toolbar.add(buttonTest);

		tabPanel = new DkTabPanel();

		final ProductEditorTab2 tab2 = new ProductEditorTab2(controller);

		final ProductEditorTab1 tab1 = new ProductEditorTab1(controller);

		final ProductEditorTab3 tab3 = new ProductEditorTab3(controller);

		final ProductEditorTab4 tab4 = new ProductEditorTab4(controller);

		tabPanel.addSelectionHandler(new SelectionHandler<Widget>()
		{
			@Override
			public void onSelection(final SelectionEvent<Widget> event)
			{
				tabPanel.forceLayout();
			}
		});
		tabPanel.add(tab1, tab1.getTabItemConfig());
		tabPanel.add(tab2, tab2.getTabItemConfig());
		tabPanel.add(tab3, tab3.getTabItemConfig());
		tabPanel.add(tab4, tab4.getTabItemConfig());

		final VerticalLayoutContainer verticalLayoutContainer = new VerticalLayoutContainer();
		verticalLayoutContainer.add(toolbar, new VerticalLayoutData(1, -1));
		verticalLayoutContainer.add(tabPanel, new VerticalLayoutData(1, 1, new Margins(2)));
		add(verticalLayoutContainer);
	}

	@Override
	public void invalidateUi()
	{
		if (isFirstReloadAfterShow())
		{
			tabPanel.setActiveWidget(tabPanel.getWidget(0));
		}

		if (controller.isEditing())
		{
			setHeadingHtml(Main.gwtSampleI18n().product_selected(controller.getEditedModel().getId(),
					controller.getEditedModel().getName()));
		}
		else
		{
			setHeadingHtml(Main.gwtSampleI18n().nothing_selected());
		}
	}

	protected abstract void onProductChanged(final ProductData productData);

	@Override
	public boolean needToBeReloaded()
	{
		return true;
	}

	@Override
	public DkBaseAction<?> getReloadSubPanelAction(final boolean force, final Object... optionalArgs)
	{
		// Nothing to be done here
		return null;
	}

	/**
	 * @return the controller
	 */
	public ProductEditorController getController()
	{
		return controller;
	}

	/**
	 * @return the tabPanel
	 */
	public TabPanel getTabPanel()
	{
		return tabPanel;
	}

}
